package com.stars.empoly.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.stars.empoly.R
import com.stars.empoly.databinding.TimeMileHourBinding
import com.stars.empoly.inte.ChoiceListData
import com.stars.empoly.widget.BaseLinearLayout
import com.trailer.base.view.BView
import com.trailer.base.view.BaseView
import java.lang.String
import java.util.*



/**
 *
 *on 2020/11/23
 */
class TimeMileView(context: Context, attrs: AttributeSet) : BaseLinearLayout(context, attrs) {

    var listTwo = ArrayList<kotlin.String>()
    var listThree = ArrayList<kotlin.String>()
    var listFour = ArrayList<kotlin.String>()
    var index = 0
    var indexEnd = 0
    var minteEnd = 0
    var hourEnd = 0


    var sendEnd = 0




    private var binding: TimeMileHourBinding? = null

    private var view: BView? = null

    init {
        init(context)

    }

    private fun init(context: Context) {
        if (context is BaseView) {
            view = context as BView
        }
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.time_mile_hour, this, true)
        setTime()
    }


    var choiceTimeRightData: ChoiceListData? = null
    fun setCallBack(choiceData: ChoiceListData) {
        this.choiceTimeRightData = choiceData
    }


    var flag = true

    fun setFlag() {
        flag = true
    }




    //设置编辑的默认时间
    fun  setTimeStart( time: kotlin.String){




        //时间
        var ss=time.split(":")
        var s1=ss[0]

        var s2=ss[1]

        var s3=ss[2]

        for ((index, value) in listTwo.withIndex()) {
            if(value == s1){
                binding!!.wheel3dTwo.currentIndex=index

            }

        }

        for ((index, value) in listFour.withIndex()) {
            if(value == s2){
                binding!!.wheel3dSix.currentIndex=index

            }

        }
        for ((index, value) in listFour.withIndex()) {
            if(value == s3){
                binding!!.wheel3dEight.currentIndex=index
                return

            }

        }

    }

    fun  setTimeEnd( time: kotlin.String){



        //时间
        var ss=time.split(":")
        var s1=ss[0]

        var s2=ss[1]
        var s3=ss[2]

        for ((index, value) in listTwo.withIndex()) {
            if(value == s1){
                binding!!.wheel3dFour.currentIndex=index

            }

        }

        for ((index, value) in listFour.withIndex()) {
            if(value == s2){
                binding!!.wheel3dSeven.currentIndex=index

            }

        }

        for ((index, value) in listFour.withIndex()) {
            if(value == s3){
                binding!!.wheel3dNine.currentIndex=index
                return

            }

        }


    }


    fun getMinuteStart(): kotlin.String {


        return listFour[minteEnd]

    }

    fun getMinuteEnd(): kotlin.String {

        return listFour[minteEnd]

    }


    fun getSendStart(): kotlin.String {


        return listFour[sendEnd]

    }

    fun getSendEnd(): kotlin.String {

        return listFour[sendEnd]

    }


    fun getTimeTwo(): kotlin.String {


        return listTwo[hourEnd]
    }

    fun getTimeThree(): kotlin.String {

        return listThree[hourEnd]

    }

    private fun setTime() {



        initTwo()
        initText()
        initThree()
        //分钟
        initSix()




    }




    private fun initText() {


        var list = ArrayList<kotlin.String>()
        list.add("至")


        binding!!.wheel3dThree.setEntries(list)


        binding!!.wheel3dThree.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));


        }

        var list1 = ArrayList<kotlin.String>()
        list1.add(":")

        binding!!.wheel3dStart.setEntries(list1)
        binding!!.wheel3dEnd.setEntries(list1)
        binding!!.wheel3dStartTo.setEntries(list1)
        binding!!.wheel3dEndTo.setEntries(list1)

//        binding!!.wheel3dStart.setOnWheelChangedListener { view, oldIndex, newIndex ->
//            val text = view.getItem(newIndex)
//
//
//
//        }
//
//        binding!!.wheel3dEnd.setOnWheelChangedListener { view, oldIndex, newIndex ->
//            val text = view.getItem(newIndex)
//
//        }




    }






    private fun initTwo() {


        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.HOUR)

        var choseIndex=0
        for (index in 0..24) {

            if(index==mHour){
                hourEnd=index
                choseIndex=index
            }
            listTwo.add(String.format(Locale.CHINA, "%02d", index))//todo"%02d:00"
        }



        binding!!.wheel3dTwo.setEntries(listTwo)




        binding!!.wheel3dTwo.currentIndex=choseIndex

        binding!!.wheel3dTwo.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text2=: $text")

            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("2", text.toString())

            }
            if (flag) {
                if (newIndex + 1 < listThree.size - 1) {
                    // listThree[newIndex + 1]
                    binding!!.wheel3dFour.currentIndex = newIndex + 1

                } else {
                    binding!!.wheel3dFour.currentIndex = listThree.size - 1
                }

            }



        }


    }


    private  fun initSix(){

        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.MINUTE)

        val mSend=mCalendar.get(Calendar.SECOND)


        var choseIndex=0
        var choseSendIndx=0
        for (index in 0..60) {
            if(index==mHour){
                choseIndex=index
                minteEnd=index

            }
            if(index==mSend){
                choseSendIndx=index
                sendEnd=index
            }

            listFour.add(String.format(Locale.CHINA, "%02d", index))//todo:"%02d:00"
        }

        binding!!.wheel3dSix.setEntries(listFour)

        binding!!.wheel3dSeven.setEntries(listFour)


        binding!!.wheel3dEight.setEntries(listFour)

        binding!!.wheel3dNine.setEntries(listFour)



        binding!!.wheel3dSix.currentIndex=choseIndex

        binding!!.wheel3dSeven.currentIndex=choseIndex

        binding!!.wheel3dEight.currentIndex=choseSendIndx

        binding!!.wheel3dNine.currentIndex=choseSendIndx


        binding!!.wheel3dSix.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text6=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("6", text.toString())

            }


        }

        binding!!.wheel3dSeven.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text6=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("7", text.toString())

            }


        }




        binding!!.wheel3dEight.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text8=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("8", text.toString())

            }


        }

        binding!!.wheel3dNine.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text9=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("9", text.toString())

            }


        }


    }






    private fun initThree() {

        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.HOUR)
        var choseIndex=0
        for (index in 0..24) {
            if(index==mHour){
                choseIndex=index
            }
            listThree.add(String.format(Locale.CHINA, "%02d", index))//todo:"%02d:00"
        }

        binding!!.wheel3dFour.setEntries(listThree)


        binding!!.wheel3dFour.currentIndex=choseIndex

        binding!!.wheel3dFour.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("3", text.toString())

            }


        }

    }

}







